const i18n = require('LanguageData');
var Game =cc.Class({
    extends: cc.Component,

    properties: {
        TouZiAnima: {
            default: null,
            type: cc.Node
        },
        BigWinAnima: {
            default: null,
            type: cc.Node
        },
        SmallWinAnima: {
            default: null,
            type: cc.Node
        },
        GameListBgAnima: {
            default: null,
            type: cc.Node
        },
        GameTimerAnima: {
            default: null,
            type: cc.Node
        },

        AudioMng: {
            default: null,
            type: cc.Node
        },
        GameKernel:{
            default: null,
            type: cc.Node
        },
        MyButton:{
            default: null,
            type: cc.Node
        },
        GameResult:{
            default: null,
            type: cc.Node
        },
        GameDJS:{
            default: null,
            type: cc.Node
        },
        GameOpenDJS:cc.Label,
        WebSocket:{
            default: null,
            type: cc.Node
        },
        Toast:{
            default: null,
            type: cc.Node 
        },

        PopSetting:{
            default: null,
            type: cc.Node 
        },
        PopDesc:{
            default: null,
            type: cc.Node
        },
        PopTips:{
            default: null,
            type: cc.Node 
        },

        userId: cc.Integer,
        token:cc.String,
        language:cc.String,
        liveid:cc.Integer,
        stream:cc.String,
       
    },

    statics: {
        instance: null
    },




     onLoad:function () {
       Game.instance=this;
        cc.log("===========onLoad1");
        //获取传参数据 
        var Request = new Object();
        Request = this.GetRequest();
        this.userId=Request["uid"]; 
        this.token=Request["token"]; 
        this.language=Request["language"];
        this.liveid=Request["liveid"]; 
        this.stream=Request["stream"];
       

        cc.log("uid:"+ this.userId);
        cc.log("token:"+this.token);
        cc.log("language:"+this.language);
       

        let lan=this.language;

        i18n.init(lan);
        
        this.LL= i18n;
        cc.log("===========init===1" );
        //初始化帧动画
        this.animGameTimer = this.GameTimerAnima.getComponent('GameLight');
        cc.log("===========init==="+this.animGameTimer );
        this.animGameTimer.init();
        this.animGameTimer.show(false);

       //初始化帧动画
        this.animFx = this.TouZiAnima.getComponent('TouZiAnima');
        this.animFx.init();
        this.animFx.show(false);
        //初始化帧动画
        this.animBigWin = this.BigWinAnima.getComponent('WinAnima');
        this.animBigWin.init();
        this.animBigWin.show(false);

         //初始化帧动画
         this.animSmallWin = this.SmallWinAnima.getComponent('WinAnima');
         this.animSmallWin.init();
         this.animSmallWin.show(false);
        
        // //初始化帧动画
        this.gameListBg = this.GameListBgAnima.getComponent('GameListBgAnima');
        cc.log("===========init==="+this.gameListBg );
        this.gameListBg.init();
        this.gameListBg.play();
    

        //初始化音频管理器
        this.audioMng=this.AudioMng.getComponent('AudioMng');
        cc.log("===========onLoad5");
      
        //播放背景音乐
        this.audioMng.playMusic();
        //押注按钮
        this.btn= this.MyButton.getComponent('MyButton');
        this.btn.init();
        cc.log("===========onLoad6");

        this.gamesLab= this.GameDJS.getComponent(cc.Label);

        //游戏数据核心
        this.gameKernel= this.GameKernel.getComponent('GameKernel');
       
        //web通讯
        this.ws=this.WebSocket.getComponent('WebSocket');
        //游戏结果
        this.gameResult=this.GameResult.getComponent('GameResult');
        this.gameResult.show(false);

        cc.log("===========onLoad7");
        //Toast
        this.mToast=this.Toast.getComponent('Toast');
        //PopTips
        this.mPopTips=this.PopTips.getComponent('PopTips');
        //弹窗-声音设置
        this.popSetting=this.PopSetting.getComponent('PopSetting');
        //弹窗-说明
        this.popDesc=this.PopDesc.getComponent('PopDesc');
        //监听键盘
        // cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        // cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);  

     },

    playWin(points){
        let flag= points[0]+points[1]+points[2];
        if (flag>10) {
            this.animBigWin.playWin();
        } else {
            this.animSmallWin.playWin();
        }
        this.gameKernel.scal(flag);
        cc.log("=========== gameKernel scal");
    },
   
   
   gameStart(){
    this.animGameTimer.play();
   },
   gameOpenJiangAnima(){
    this.GameDJS.active=false;
    this.animFx.play();
    this.audioMng.playCard();

   },
   gameOpenJiangResult(){
    let points= this.gameKernel.points;
    this.animFx.show(false);
    this.gameResult.gameOpenJiang(points);
    this.playWin(points);
   

   },
   gameOpenJiangResult2(){
    let bigWinScore= this.gameKernel.bigWinScore;
    let smallWinScore= this.gameKernel.smallWinScore;

    let bigWin=(bigWinScore>0?"+":'')+ this.gameKernel.num1000(bigWinScore);
    let smallWin=(smallWinScore>0?"+":'')+  this.gameKernel.num1000(smallWinScore);

    let bigBetScore= this.gameKernel.bigBetScoreTotal;
    let smallBetScore= this.gameKernel.smallBetScoreTotal;
    let bigBet= this.gameKernel.num1000(bigBetScore);
    let smallBet= this.gameKernel.num1000(smallBetScore);

    this.audioMng.playWin(); 
    this.gameResult.showWin2Ya(bigWin,smallWin,bigBet,smallBet);
    

    return


    
        // let winScore= this.gameKernel.winScore;
        // cc.log("winScore="+winScore);
        // if (winScore>0) {
        //     let score= this.gameKernel.num1000(winScore);
        //     this.gameResult.showWin(score);
        //     this.audioMng.playWin();
            
        // } else if(winScore<0) {
        //     this.gameResult.showLose();
        //     this.audioMng.playLose();
        //     Game.instance.mToast.show('lose'+winScore,3);

        // }
    
   },
   gameReset(){
   
    this.animBigWin.show(false);
    this.animSmallWin.show(false);
    this.gameResult.hide();
    this.gameKernel.newGame();
   },

   gameStartHide(){
    this.animBigWin.show(false);
    this.animSmallWin.show(false);
    this.gameResult.hide();
   },
   


gameCountdown(times){
    
    if (times>0) {
        this.GameDJS.active=true;
        this.gamesLab.string = times;
    }else{
        this.GameOpenDJS.string=times+10
    }
    

    if (times>5&&times<=10) {
        this.audioMng.playDi();
        this.animGameTimer.play2(); 
        this.gameKernel.scalDJS(this.GameDJS);
    }
    if (times==5) {
        this.audioMng.playStopBet();
    }
    if (times>=0&&times<=5) {
        this.animGameTimer.show(false);
    }
    
},

GetRequest() {//这个函数，其实就是js的方法，在我的博客中能找到出处的
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
      var str = url.substr(1);
      var strs = str.split("&");
      for(var i = 0; i < strs.length; i ++) {
        theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
      }
    }
    return theRequest;
  },
   popSettingShow(show){
    this.popSetting.node.active=show;
   },
   
   popDescShow(show){
    this.popDesc.node.active=show;
   }
   

});
